package main

import (
	"errors"
	resp "tacitus/library/response"
	"tacitus/modules/cont"
	"tacitus/modules/dict"
	"tacitus/modules/guestbook"
	"tacitus/modules/upload"
	"tacitus/modules/user"
	"tacitus/modules/video"

	//errors2 "errors"
	"fmt"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"tacitus/library"
	errs "tacitus/library/error"
)

func main() {
	go func() {
		// 启动前端静态页面服务
		e := echo.New()
		e.Logger.SetOutput(lib.DebugLog)
		e.Static("/", "file/page")
		e.Logger.Fatal(e.Start(":80"))
	}()

	e := echo.New()
	e.Logger.SetOutput(lib.DebugLog)

	e.Use(middleware.CORS())
	e.Use(resp.ErrorHandler)

	e.Static("/file", "file/upload")

	RouteTestApi(e)

	dict.RouteApi(e.Group("/dict"))

	user.RouteApi(e.Group("/user"))
	user.RouteAdminApi(e.Group("/admin/user"))
	user.RouteMobileApi(e.Group("/user/mobile"))
	user.RouteHeadApi(e.Group("/user/head"))

	guestbook.RouteApi(e.Group("/guestbook"))
	guestbook.RouteAdminApi(e.Group("/admin/guestbook"))

	upload.RouteApi(e.Group("/upload"))

	cont.RouteApi(e.Group("/cont"))
	cont.RouteAdminApi(e.Group("/admin/cont"))

	video.RouteApi(e.Group("/video"))
	video.RouteAdminApi(e.Group("/admin/video"))

	fmt.Println("初始化成功，启动服务")
	e.Logger.Fatal(e.Start(":"+lib.Config.Port["dev"]))
}

func RouteTestApi(e *echo.Echo)  {
	e.POST("/err/called", func(c echo.Context) error {
		return errs.Called("调用错误")
	})
	e.POST("/err/report", func(c echo.Context) error {
		return errs.Report("上报错误", errors.New("report"))
	})
	e.POST("/err/final", func(c echo.Context) error {
		return errors.New("final")
	})
	e.POST("/err/panic", func(c echo.Context) error {
		panic(map[string]string{
			"a":"yujtrhjtrfwef",
			"b":"uytf834hre",
		})
	})
}